{% extends "../../../../../view/layouts/admin.html" %} 
{% block content %}
<header class="header navbar bg-white shadow">
	<div class="btn-group tool-button">
		<a class="btn btn-primary navbar-btn" href="/app/ad/admin/index">返回</a>
	</div>
</header>

<div class="content-wrap">
	<div class="wrapper" style="min-height:500px;">
		<section class="panel panel-form pt25">
			<form id="addForm" role="form" class="form-horizontal parsley-form" data-parsley-validate action="/app/ad/admin/edit" method="post">
				<div class="row mb10">
					<div class="col-lg-12">
						<input type="hidden" id="id" name="id" value="{{ad.id}}">
						<div class="form-group">
							<label for="code" class="col-sm-2 control-label">所在广告位</label>

							<div class="col-sm-8">
								<select name="position" id="position" class="form-control">
									{% for item in positions %}
									<option value="{{item.id}}" {%if item.id == ad.position_id%}selected{%endif%}>{{item.name}}</option>
									{% endfor %}
								</select>
								
							</div>
						</div>
						<div class="form-group">
							<label for="name" class="col-sm-2 control-label">广告名称</label>

							<div class="col-sm-8">
								<input type="text" id="name" class="form-control" name="name" data-parsley-required="true" placeholder="广告名称" value="{{ad.name}}">
							</div>
						</div>
						

						<div class="form-group">
							<label for="code" class="col-sm-2 control-label">广告图片</label>

							
							<div class="col-sm-8 picture-upload">
								<div id="queue1"></div>
								<div id="img1" class="view">
									{% if ad.pic_url%}
									<li><img src='{{ad.pic_url}}'/><i class='ti-trash'  onclick="delImg(this)"></i></li>
									{% endif %}
								</div>
								<input id="pic_url" type="file" multiple="false">
								<input type="text"  name="pic_url" value="{{ad.pic_url}}"  class="form-control" placeholder="图片地址(不上传可直接调用网络图片)"/>
								<div class="clearfix"></div>
							</div>

						</div>
						

						<div class="form-group">
							<label for="url" class="col-sm-2 control-label">链接地址</label>

							<div class="col-sm-8">
								<input type="text" id="url" class="form-control" name="url" data-parsley-required="false" placeholder="链接地址" value="{{ad.url}}">
							</div>
						</div>
						
						

					</div>
				</div>
				<div class="col-lg-3"></div>
				<div class="col-lg-6">
					<div class="form-group text-center">
						<label></label>

						<div>
							<button class="btn btn-primary btn-block btn-lg btn-parsley" data-loading-text="正在提交...">提 交</button>
						</div>
					</div>
				</div>
			</form>
		</section>

	</div>
</div>

<script language="JavaScript">
	$(document).ready(function() {
	
		$('#addForm').ajaxForm({
			dataType: 'json',
			beforeSubmit: function(arr, form, options) {
				
				form.find("button:submit").button("loading");
				
			},
			success: function(data, statusText, xhr, form) {
				if(data.errno == 0) {
					Toast.success(data.data);
					//刷新父级菜单
					//form.resetForm();
				}
				else {
					Toast.error(data.errmsg);
				}
				form.find("button:submit").button("reset");
			}
		});

	});

	function delImg(t) {
		$(t).parent().remove();
	}

	$(function() {

		$('#pic_url').uploadifive({
			'auto': true,
			'multi': false,
			'width': '100%',
			'fileObjName': 'picture',
			'buttonText': '<i class="ti-plus"></i>',
			'fileType': 'image/*',
			'fileSizeLimit': 1024,
			'queueSizeLimit': 1,
			'formData': {},
			'queueID': 'queue1',
			'uploadScript': '/app/ad/admin/upload',
			'onUploadComplete': function(file, data) {
				data = JSON.parse(data);
				console.log(data)
				if(data.errno == 0) {
					Toast.success("上传成功");

					var html = "<li><img src='" + data.data[0].url + "'/><i class='ti-trash'  onclick=\"delImg(this)\"></i></li>"

					
					
					$("#img1").append(html);
					$("[name='pic_url']").val(data.data[0].url);
					

				}
				else {
					Toast.error(data.errmsg);
				}
			},
			'onSelect': function(queue) {}
		});
		
	});
</script>
{% endblock %}